//index.js
//获取应用实例
Page({
  //数据
  data: {
    login:{
      user_id:""
    },
    current:0,
    current2:0,
    currentLength:3,
    bannerList:[],
    product_list:[],
    shop_list:[],
    area_id:"",
    area_name:"",
    proC:"",
    cityC:"",
    proA:"",
    cityA:"",
    proIndex:"",
    cityIndex:"",
    isLogin:false,
        indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 1000,
  },

  //操作
  //页面加载
  onLoad:function(){
    if(!wx.getStorageSync("user_id")){
      this.data.isLogin=true;
      this.setData({
        isLogin:true
      })
    }
    this.data.login.user_id= wx.getStorageSync('user_id');
    var self=this;
    wx.hideLoading();
    var cityID="";
    if(wx.getStorageSync("city_id")){
      cityID=wx.getStorageSync("city_id");
    }
    wx.request({
      url:"https://wx.ichelaba.com/htapi/index.php/store/index",
      data:{
        area_id:cityID
      },
      method:'GET',
      success:function(res){
        console.log(res);
        self.data.area_id=res.data.area_id;
        self.setData({
          area_name:res.data.area_name
        })
        console.log(res.data.area_name)
        wx.setStorageSync("city_id",res.data.area_id);
        wx.setStorageSync("city_name",res.data.area_name);
        var bannerList=[];
        var product_list=[];
        var shop_list=[];
        if(res.data){
          for(var i=0;i<res.data.banner_list.length;i++){
            bannerList.push({})
            for(var key in res.data.banner_list[i]){
              bannerList[i][key]=res.data.banner_list[i][key]
            }
          }
          for(var i=0;i<res.data.product_list.length;i++){
            product_list.push({})
            for(var key in res.data.product_list[i]){
              product_list[i][key]=res.data.product_list[i][key]
            }
          }
          // for(var i=0;i<res.data.shop_list.length;i++){
          //   shop_list.push({})
          //   for(var key in res.data.shop_list[i]){
          //     shop_list[i][key]=res.data.shop_list[i][key]
          //   }
          // }
          self.data.currentLength=res.data.shop_list.length;
          console.log(res.data.shop_list)
          self.setData({
            bannerList:bannerList,
            product_list:product_list,
            shop_list:res.data.shop_list
          })
        }
        // wx.request({
        //   url:"https://wx.ichelaba.com/htapi/index.php/user/address/add",
        //   data:{
        //     user_id:wx.getStorageSync("user_id")
        //   },
        //   method:"GET",
        //   success:function(res){
            self.data.proC=res.data.province_list;
            var proA=[];
            for(var i=0;i<res.data.province_list.length;i++){
              proA.push(res.data.province_list[i].district_name);
            }
            self.setData({
              proIndex:0,
              proA:proA
            })
            wx.request({
              url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
              data:{
                pid:self.data.proC[0].district_id
              },
              method:"GET",
              success:function(res){
                self.data.cityC=res.data.list;
                var cityA=[];
                for(var i=0;i<res.data.list.length;i++){
                  cityA.push(res.data.list[i].district_name);
                }
                self.setData({
                  cityIndex:0,
                  cityA:cityA
                })
                wx.hideLoading();
              }
            })
          // }
        // })
      }
    });
    wx.showLoading({
      title:"加载中...",
      mask:false,
      success:function(e){
        console.log(e)
      }
    });
  },
  //切换
  swiperChange:function(e){
    this.setData({
      current2:e.detail.current
    })
  },
  swiperPrev:function(e){
    var num=this.data.current2-1;
    if(num<0){
      num=0
    }
    this.setData({
      current:num
    })
  },
  swiperNext:function(e){
    var num=this.data.current2+1;
    if(num>=this.data.currentLength){
      num=this.data.currentLength-1
    }
    this.setData({
      current:num
    })
  },
  isLogin_FN:function(e){
    // if(this.data.isLogin){
    //   wx.showModal({
    //     title:"提示",
    //     content:"请先登录",
    //     showCancel:false
    //   })
    //   return;
    // }
  },
  columnchange_FN:function(e){
    var self=this;
    if(e.detail.column==0){
      self.setData({
        proIndex:e.detail.value
      })
      wx.request({
        url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
        data:{
          user_id:wx.getStorageSync("user_id"),
          pid:self.data.proC[e.detail.value].district_id
        },
        method:"GET",
        success:function(res){
          self.data.cityC=res.data.list;
          var cityA=[];
          for(var i=0;i<res.data.list.length;i++){
            cityA.push(res.data.list[i].district_name);
          }
          self.setData({
            cityIndex:0,
            cityA:cityA
          })
        }
      })
    }
  },
  change_FN:function(e){
    var thisIndex="";
    if(e.detail.value[1]==null){
      thisIndex=0;
    }else{
      thisIndex=e.detail.value[1]
    }
    console.log(this.data.cityC[thisIndex].district_id)
    wx.setStorageSync("city_id",this.data.cityC[thisIndex].district_id);
    wx.setStorageSync("city_name",this.data.cityC[thisIndex].district_name);
    wx.reLaunch({
      url:"/pages/index/index"
    })
  }
})
